package l1j.server.server.command.executor;

import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.NewSystem.各項記錄輸出;
import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.L1Inventory;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;

public class L1SmallGM implements L1CommandExecutor{
	private static Logger _log = Logger.getLogger(L1SmallGM.class.getName());
	public static L1CommandExecutor getInstance() {
		return new L1SmallGM();
	}
	private  final StringBuilder _StLog =new StringBuilder(500);

	private StringBuilder _ItemNameLog =new StringBuilder();

	private L1SmallGM() {
	}

	private boolean createNewItem(L1PcInstance pc, int item_id, int count) {
		final L1ItemInstance item = ItemTable.getInstance().createItem(item_id);
		if (item != null) {
			item.setCount(count);
			if (pc.getInventory().checkAddItem(item, count) == L1Inventory.OK) {
				pc.getInventory().storeItem(item);
			} else { // 持てない場合は地面に落とす 處理のキャンセルはしない（不正防止）
				L1World.getInstance().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
			}
			this._ItemNameLog.append(item.getLogName()).append(" 流水號:").append(item.getId()).append(" 創出並發送記錄檔\n");
		//	pc.sendPackets(new S_ServerMessage(403, item.getLogName())); // %0を手に入れました。
			this._StLog.append(item.getLogName()).append(" 流水號:").append(item.getId()).append("\r\n");
			return true;
		} else {
			return false;
		}
	}
	// @Override
	@Override
	public void execute(final L1PcInstance pc, final String cmdName, final String arg) {
		try {
			final StringTokenizer st = new StringTokenizer(arg);
			 String type=null;
			if(st.hasMoreTokens()){
				type = st.nextToken();
				}else{
					pc.sendPackets(new S_SystemMessage("請輸入:武器、防具、推推金幣、支票"));
					return;
				}
			 String nameid = null;

			 this._StLog.append("(").append(pc.getNetConnection().getIp().toString()).append(") 小G ").append(pc.getName()).append("創造了\r\n");

			 if(type.startsWith("推推金幣")){
					this.createNewItem(pc,80308,1000);
				}else if(type.startsWith("支票")){
					this.createNewItem(pc,60308,1000);
				}else{

			 if(st.hasMoreTokens()){
				 nameid = st.nextToken();
			 }else{
				 pc.sendPackets(new S_SystemMessage("請輸入: 寶區編號(1~8)"));
					return;
				}
			 final int typeid =Integer.parseInt(nameid);
			if(type.startsWith("武器")){
				final int[] weaponid ={346,321,371,396,471,496,421,446};

				switch(typeid){

				case 1: // 平風
					for (final int element : weaponid) {
					this.createNewItem(pc,element,1);
					}
					break;
				case 2: // 翻雲
					for (final int element : weaponid) {
						this.createNewItem(pc,element+1,1);
					}
					break;
				case 3: // 覆雨
					for (final int element : weaponid) {
						this.createNewItem(pc,element+2,1);
					}
					break;
				case 4: // 操雪
					for (final int element : weaponid) {
						this.createNewItem(pc,element+3,1);
					}
					break;
				case 5: // 貫雷
					for (final int element : weaponid) {
						this.createNewItem(pc,element+4,1);
					}
					break;
				case 6: // 陽炎
					for (final int element : weaponid) {
						this.createNewItem(pc,element+5,1);
					}
					break;
				case 7: // 斷空
					for (final int element : weaponid) {
						this.createNewItem(pc,element+6,1);
					}
					break;
				case 8: // 瘋狗
					for (final int element : weaponid) {
						this.createNewItem(pc,element+7,1);
					}
					break;
				default:
					pc.sendPackets(new S_SystemMessage("請輸入: .創造 武器 編號(1~8)"));
					break;
				}

			}else if(type.startsWith("防具")){
				final int[] armorid ={21694,21701,21708,21715,21722,21729};

				switch(typeid){
				case 1: //打神
					for (final int element : armorid) {
						this.createNewItem(pc,element,1);
						}
					break;
				case 2: //鬥神
					for (final int element : armorid) {
						this.createNewItem(pc,element+1,1);
						}
					break;
				case 3: //斬神
					for (final int element : armorid) {
						this.createNewItem(pc,element+2,1);
						}
					break;
				case 4: //諸神
					for (final int element : armorid) {
						this.createNewItem(pc,element+3,1);
						}
					break;
				case 5: //滅神
					for (final int element : armorid) {
						this.createNewItem(pc,element+4,1);
						}
					break;
				case 6: //弒神
					for (final int element : armorid) {
						this.createNewItem(pc,element+5,1);
						}
					break;
				case 7: //封神
					for (final int element : armorid) {
						this.createNewItem(pc,element+6,1);
						}
					break;
				case 8: //瘋狗
					for(int i=21776; i<21782;i++){
						this.createNewItem(pc,i,1);
						}
					break;
				default:
					pc.sendPackets(new S_SystemMessage("請輸入: .創造 防具 編號(1~8)"));
					break;
				}
			}else{
				pc.sendPackets(new S_SystemMessage("請輸入:武器、防具、推推金幣、支票"));
				return;
			}
				}
				 各項記錄輸出.LogSave(7,this._StLog.toString());
				pc.sendPackets(new S_SystemMessage(this._ItemNameLog.toString()));
				this._ItemNameLog=null;
				this._StLog.delete(0, this._StLog.length());
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
	}
}
